-- PixySpring
-- Created by chengb Jul/29/2016
-- 妖精之泉

-- 初始化
function initPixySpring(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 音效
            AudioM.playFx("ui_open1");

            -- 已经领取过奖励了
            if BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
                self:openBuildingView(nil, nil, buildingInfo["dialog2"], buildingInfo["view_text2"]);
                return;
            end

            local function okFunc()
                local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
                local classId = dbase["submit_item"];
                if type(classId) == "number" then
                    if ItemM.getAmount(ME.user, classId) <= 0 then
                        -- 没有相应道具
                        local failTip = buildingInfo["fail_tip"]
                        if type(failTip) == 'string' and string.len(failTip) > 0 then
                            alert(failTip);
                        end
                        return;
                    end
                end

                DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
                --if DungeonActionM.go("pixy_spring", self.gridData:getPos()) then
                --    local tip = buildingInfo["bonus_tip"]
                --    if type(tip) == 'string' and string.len(tip) > 0 then
                --        local alertList = splitString(tip, "\n");
                --        splitAlert(alertList);
                --    end
                --end
            end

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
end
